<?php
	namespace ampleharvest\task\metric;

	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/abstract_classes/abs_Task.php";
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/classes/Tables/cTABLE_Data.php";
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/classes/Tables/cTABLE_Metric.php";
	require_once dirname(dirname(dirname(dirname(__FILE__)))) . "/classes/Tables/cTABLE_SourceList.php";

	class count_Source extends \abs_Task
	{
		const C_STR_NAME = "count_Source";

		private $m_obj_TABLE_SourceList = null;

		public function __construct($str_LockPath)
		{
			$this->set_LockPath($str_LockPath);
			$this->set_Name(self::C_STR_NAME);
			$this->m_obj_TABLE_SourceList = new \cTABLE_SourceList();
			parent::__construct();
		}

		public function run($bool_Force = false)
		{
			$this->set_Force($bool_Force);
			if (!parent::run())
			{
				return false;
			}
			$arr_Data = array();

			$obj_CurrentDate = new \DateTime();

			$obj_StartHour = clone $obj_CurrentDate;

			$obj_CurrentDate->setTime(0, 0, 0);

			$obj_StartYear = clone $obj_CurrentDate;
			$obj_StartYear->setDate($obj_CurrentDate->format("Y"), 1, 1);

			$obj_StartMonth = clone $obj_CurrentDate;
			$obj_StartMonth->setDate($obj_CurrentDate->format("Y"), $obj_CurrentDate->format("m"), 1);

			$obj_StartWeek = clone $obj_CurrentDate;
			$obj_StartWeek = $this->get_PriorDayOfTheWeek($obj_StartWeek, 1);

			$obj_StartDay = clone $obj_CurrentDate;

			$obj_StartHour->setTime($obj_StartHour->format("H"), 0, 0);
			/*
			 print("Current date - " . $obj_CurrentDate->format("c") . "\n");
			 print("StartYear - " . $obj_StartYear->format("c") . "\n");
			 print("StartMonth - " . $obj_StartMonth->format("c") . "\n");
			 print("StartWeek - " . $obj_StartWeek->format("c") . "\n");
			 print("StartDay - " . $obj_StartDay->format("c") . "\n");
			 print("StartHour - " . $obj_StartHour->format("c") . "\n");
			 */

			$arr_Data["hour"] = $this->collect_Count($obj_StartHour);
			$arr_Data["day"] = $this->collect_Count($obj_StartDay);
			$arr_Data["week"] = $this->collect_Count($obj_StartWeek);
			$arr_Data["month"] = $this->collect_Count($obj_StartMonth);
			$arr_Data["year"] = $this->collect_Count($obj_StartYear);
			$arr_Data["total"] = $this->collect_Count();

			$arr_ProcData = array();

			foreach ($arr_Data as $str_SubCat => $arr_Return)
			{
				foreach ($arr_Return as $arr_Source)
				{
					if (!isset($arr_ProcData[$arr_Source["source"]]))
					{
						$arr_ProcData[$arr_Source["source"]] = array();
					}

					$arr_ProcData[$arr_Source["source"]][$str_SubCat] = $arr_Source["count"];
				}
			}

			//print_r($arr_ProcData);

			foreach ($arr_ProcData as $str_Source => $arr_Source)
			{
				$this->m_obj_TABLE_SourceList->clear();
				$this->m_obj_TABLE_SourceList->clear_Query();

				$this->m_obj_TABLE_SourceList->add_ColumnQuery($this->m_obj_TABLE_SourceList->m_int_COL_Source, $str_Source);

				if ($this->m_obj_TABLE_SourceList->do_Search() > 0)
				{
					$arr_Results = $this->m_obj_TABLE_SourceList->get_ResultSet();
					if (isset($arr_Results[0]))
					{
						$this->m_obj_TABLE_SourceList->load_DataFromArray($arr_Results[0]);
					}
				}

				$obj_Date = new \DateTime();
				$this->m_obj_TABLE_SourceList->set_datetime($obj_Date->format("Y-m-d H:i:s"));
				$this->m_obj_TABLE_SourceList->set_source($str_Source);
				if (!isset($arr_Source["hour"]))
				{
					$arr_Source["hour"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_hourly($arr_Source["hour"]);

				if (!isset($arr_Source["day"]))
				{
					$arr_Source["day"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_daily($arr_Source["day"]);

				if (!isset($arr_Source["week"]))
				{
					$arr_Source["week"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_weekly($arr_Source["week"]);

				if (!isset($arr_Source["month"]))
				{
					$arr_Source["month"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_monthly($arr_Source["month"]);

				if (!isset($arr_Source["year"]))
				{
					$arr_Source["year"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_yearly($arr_Source["year"]);

				if (!isset($arr_Source["total"]))
				{
					$arr_Source["total"] = 0;
				}
				$this->m_obj_TABLE_SourceList->set_total($arr_Source["total"]);

				$this->m_obj_TABLE_SourceList->save_Data2DB();
			}

			return true;
		}

		private function get_PriorDayOfTheWeek(\DateTime $obj_Current, $int_DesiredDOW)
		{
			$int_DOW = $obj_Current->format("w");

			$int_Offset = ($int_DOW - $int_DesiredDOW);

			if ($int_Offset < 0)
			{
				$int_Offset = 7 + $int_Offset;
			}

			$obj_Current->setDate($obj_Current->format("Y"), $obj_Current->format("m"), $obj_Current->format("d") - $int_Offset);

			return ($obj_Current);
		}

		private function collect_Count(\DateTime $obj_StartDate = null, \DateTime $obj_EndDate = null)
		{
			$obj_TABLE_Data = new \cTABLE_Data();

			$obj_TABLE_Data->add_ReturnFunction("count(*)", "count");
			$obj_TABLE_Data->add_ReturnCol($obj_TABLE_Data->m_int_COL_Source);
			$obj_TABLE_Data->add_Group($obj_TABLE_Data->m_int_COL_Source);
			if ($obj_StartDate)
			{
				$obj_TABLE_Data->add_ColumnQuery($obj_TABLE_Data->m_int_COL_IndexDate, $obj_StartDate->format("Y-m-d H:i:s"),
					\cCondition::c_str_OPERATOR_GREATER);
			}

			$obj_TABLE_Data->do_Search();

			return $obj_TABLE_Data->get_ResultSet();
		}
	}
?>